hhchift
08/02/2022, 9:00 PMrp_st
08/03/2022, 5:29 AMhhchift
08/03/2022, 1:00 PMrp_st
08/03/2022, 1:33 PMhhchift
08/04/2022, 2:36 PMrp_st
08/04/2022, 2:40 PMrp_st
08/04/2022, 2:40 PMhhchift
08/05/2022, 10:11 AMhhchift
08/05/2022, 10:11 AMrp_st
08/05/2022, 10:18 AMhhchift
08/05/2022, 10:23 AMrp_st
08/05/2022, 10:24 AMrp_st
08/05/2022, 10:24 AMrp_st
08/05/2022, 11:38 AMaccountSelected: "accountID"
- Now, when associating a role to the user's account, you can call the UserRoles.addRoleToUser
function with the userId
value as userId+accountId
. And so this way, you are scoping the roles for that user to be per account. So whenever you want to get a list of roles that the user has, you would need to pass in the string userId+accountId
to the UserRoles
functions. And you could get the current selected accountId from the session.hhchift
08/05/2022, 11:42 AMrp_st
08/05/2022, 11:42 AMrp_st
08/05/2022, 11:42 AMhhchift
08/05/2022, 11:43 AMrp_st
08/05/2022, 11:43 AMhhchift
08/05/2022, 11:43 AMrp_st
08/05/2022, 11:43 AMhhchift
08/05/2022, 2:10 PMrp_st
08/05/2022, 2:11 PMrp_st
08/05/2022, 2:12 PMhhchift
08/05/2022, 2:12 PMhhchift
08/05/2022, 2:13 PMrp_st
08/05/2022, 2:14 PMrp_st
08/05/2022, 2:15 PMhhchift
08/05/2022, 5:13 PMhhchift
10/12/2022, 2:22 PMrp_st
10/12/2022, 2:23 PMhhchift
10/12/2022, 2:24 PMrp_st
10/12/2022, 2:24 PMhhchift
10/12/2022, 2:28 PMrp_st
10/12/2022, 2:28 PMhhchift
10/12/2022, 2:28 PMrp_st
10/12/2022, 2:29 PMhhchift
10/12/2022, 2:30 PMhhchift
10/12/2022, 2:30 PMrp_st
10/12/2022, 2:34 PMawait session.fetchAndSetClaim(UserRoleClaim)
as mentioned here: https://supertokens.com/docs/userroles/managing-roles-and-users
Now the UserRoleClaim
internally will call the getRolesForUser function from the roles recipe. The input to this function is the userId + userContext object. You want to override this function to take the input userId and get the session from the userContext (which you will have to add to it), and then form the actual ID (userId+accountId) and then call the original implementaiton.rp_st
10/12/2022, 2:34 PMhhchift
10/12/2022, 2:36 PMhhchift
10/12/2022, 2:36 PMrp_st
10/12/2022, 2:37 PMrp_st
10/12/2022, 2:37 PMhhchift
10/12/2022, 2:37 PMhhchift
10/12/2022, 2:37 PMrp_st
10/12/2022, 2:38 PMrp_st
10/12/2022, 2:44 PMts
UserRoles.init({
override: {
functions: (oI) => {
return {
...oI,
addRoleToUser: async function (input) {
let sessionContainer: SessionContainer = input.userContext.session;
if (sessionContainer === undefined) {
throw new Error("Don't know which account the user has selected currently");
}
let currentAccountId = sessionContainer.getAccessTokenPayload()["currAccountId"];
input.userId = input.userId + "|" + currentAccountId
return oI.addRoleToUser(input);
},
getRolesForUser: async function (input) {
let sessionContainer: SessionContainer = input.userContext.session;
if (sessionContainer === undefined) {
throw new Error("Don't know which account the user has selected currently");
}
let currentAccountId = sessionContainer.getAccessTokenPayload()["currAccountId"];
input.userId = input.userId + "|" + currentAccountId
return oI.getRolesForUser(input);
},
getUsersThatHaveRole: async function (input) {
let resp = await oI.getUsersThatHaveRole(input);
if (resp.status === "OK") {
resp.users = resp.users.map(i => {
return i.split("|")[0];
});
// remove duplicate userIds from the array
resp.users = [...new Set(resp.users)];
}
return resp;
}
}
}
}
})
hhchift
10/12/2022, 2:45 PMhhchift
10/12/2022, 2:46 PMhhchift
10/12/2022, 2:46 PMrp_st
10/12/2022, 2:47 PMhhchift
10/12/2022, 2:48 PMhhchift
10/12/2022, 2:48 PMhhchift
10/12/2022, 2:48 PMrp_st
10/12/2022, 2:49 PMhhchift
10/12/2022, 2:52 PMhhchift
10/12/2022, 2:52 PMhhchift
10/12/2022, 2:53 PMhhchift
10/12/2022, 2:53 PMrp_st
10/12/2022, 2:54 PMinput
which contains these three things - but just in the object formhhchift
10/12/2022, 2:54 PMrp_st
10/12/2022, 2:55 PMhhchift
10/12/2022, 3:01 PMhhchift
10/12/2022, 3:01 PMrp_st
10/12/2022, 3:03 PMhhchift
10/12/2022, 3:27 PMhhchift
10/12/2022, 3:27 PMrp_st
10/12/2022, 3:28 PMhhchift
10/12/2022, 3:28 PMrp_st
10/12/2022, 3:28 PMrp_st
10/12/2022, 3:28 PMhhchift
10/12/2022, 3:29 PMhhchift
10/12/2022, 3:29 PMrp_st
10/12/2022, 3:29 PMhhchift
10/12/2022, 4:22 PMhhchift
10/12/2022, 4:22 PMhhchift
10/12/2022, 4:23 PMhhchift
10/12/2022, 4:24 PMrp_st
10/12/2022, 4:24 PMrp_st
10/12/2022, 4:24 PMrp_st
10/12/2022, 4:24 PMhhchift
10/12/2022, 4:24 PMrp_st
10/12/2022, 4:24 PMrp_st
10/12/2022, 4:25 PMhhchift
10/12/2022, 4:29 PMhhchift
10/12/2022, 4:29 PMrp_st
10/12/2022, 4:37 PMattemptRefreshingSession
rp_st
10/12/2022, 4:37 PMhhchift
10/13/2022, 11:42 AMhhchift
10/13/2022, 11:42 AMhhchift
10/13/2022, 11:42 AMhhchift
10/13/2022, 11:42 AMrp_st
10/13/2022, 11:56 AMrp_st
10/13/2022, 11:56 AMhhchift
10/13/2022, 12:28 PMhhchift
10/13/2022, 12:28 PMhhchift
10/13/2022, 12:29 PMhhchift
10/13/2022, 12:29 PMrp_st
10/13/2022, 12:29 PMhhchift
10/13/2022, 12:45 PMsignOutPOST: async function (input) {
console.log("signint out")
if (originalImplementation.signOutPOST === undefined) {
throw Error("Should never come here");
}
// First we call the original implementation of signUpPOST.
console.log('signing out')
let response = await originalImplementation.signOutPOST(input);
// make sure the roles are incorporated
return response;
},
rp_st
10/13/2022, 12:48 PMhhchift
10/13/2022, 12:50 PMSession.init({
override: {
functions: (originalImplementation) => {
return {
...originalImplementation, `
hhchift
10/13/2022, 12:50 PMrp_st
10/13/2022, 12:50 PMfunctions
to apis
hhchift
10/13/2022, 12:50 PMhhchift
10/13/2022, 12:50 PMhhchift
10/13/2022, 12:51 PMrp_st
10/13/2022, 12:51 PMrp_st
10/13/2022, 12:51 PMhhchift
10/13/2022, 3:46 PMhhchift
10/13/2022, 3:47 PMhhchift
10/13/2022, 3:47 PMlet response = await originalImplementation.createNewSession(input);
await response.fetchAndSetClaim(UserRoleClaim, { 'session': response });
await response.fetchAndSetClaim(PermissionClaim, { 'session': response });
return response;
hhchift
10/13/2022, 3:47 PMrp_st
10/13/2022, 3:50 PMhhchift
10/13/2022, 3:53 PMhhchift
10/13/2022, 3:53 PMhhchift
10/13/2022, 3:53 PMhhchift
10/13/2022, 3:53 PMhhchift
10/13/2022, 3:53 PMhhchift
10/13/2022, 3:53 PMrp_st
10/13/2022, 3:53 PMrp_st
10/13/2022, 3:54 PMresponse.getAccessTokenPayload()
before each of the fetchAndSetClaim
- what do you get?hhchift
10/13/2022, 4:35 PMhhchift
10/13/2022, 4:36 PM{
'st-role': { v: [], t: 1665678920120 },
'st-perm': { v: [], t: 1665678919479 },
currAccountId: 'e3892d4b-4186-4f75-9efe-6bb41eca1c8f'
}
hhchift
10/13/2022, 4:36 PMrp_st
10/13/2022, 4:36 PMrp_st
10/13/2022, 4:37 PMsession
object that's being passed into it?hhchift
10/13/2022, 4:38 PMhhchift
10/13/2022, 4:38 PMhhchift
10/13/2022, 4:38 PMhhchift
10/13/2022, 4:38 PMrp_st
10/13/2022, 4:38 PMvvaf.
10/28/2022, 5:11 AMrp_st
10/28/2022, 5:13 AM